package com.sunriz.refreshhcdata.exception;

import com.sunriz.refreshhcdata.POJO.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.sunriz.refreshhcdata.exception
 * Description:
 *
 * @Author BuTang
 * @Create 2024/11/14 11:05
 * @Version 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理所有异常
     * @param e exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result> handleException(Exception e) {
        // 打印异常堆栈信息，使用 SLF4J 进行日志记录
        log.error("Exception occurred: {}", e.getMessage(), e);

        // 构建自定义的错误信息
        Result result = new Result();
        result.setCode("500");
        result.setErrorMsg("服务器发生错误：" + e.getMessage());

        // 返回包含错误信息的 ResponseEntity 对象，状态码为 500（Internal Server Error）
        return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);    }
}
